package com.fruit.controller.app.login;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fruit.controller.base.BaseController;
import com.fruit.pojo.user.UserLogin;
import com.fruit.service.user.UserManager;
import com.fruit.util.ResultInfo;
import com.fruit.util.ResultUtil;
import com.fruit.util.ValidateUtil;

/**
 * 用户登录controller
 * @author Administrator
 * @version 2017.12.01 11:40
 */
@Controller
@RequestMapping(value = "/app/")
public class LoginController extends BaseController {

	@Autowired
	private UserManager userService;

	/**
	 * app快捷登录
	 * @param ul UserLogin
	 * @param request request
	 * @return Map<String, Object>
	 */
	@RequestMapping(value = "loginToCode", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> loginToCode(UserLogin ul, HttpServletRequest request) {
		this.logBefore(logger, "快捷登录");
		Map<String, Object> resMap = new HashMap<String, Object>();
		String type = request.getParameter("type");
		String channel = request.getParameter("channel");
		try {
			if (StringUtils.isEmpty(type) || StringUtils.isEmpty(channel)) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "no-type", "参数有误!"));
				return resMap;
			}
			ResultInfo ri = ValidateUtil.phone(ul.getPhone());
			if (ri != null) {
				resMap.put("result", ri);
				return resMap;
			}
			ri = ValidateUtil.smsCode(ul.getSmsCode());
			if (ri != null) {
				resMap.put("result", ri);
				return resMap;
			}
			resMap = userService.updateCodeToLogin(ul, type, channel);

		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
		}
		this.logAfter(logger);
		return resMap;
	}

	/**
	 * app账号登录
	 * @param ul UserLogin
	 * @param request request
	 * @return Map<String, Object>
	 */
	@RequestMapping(value = "loginToPwd", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> loginToPwd(UserLogin ul, HttpServletRequest request) {
		this.logBefore(logger, "账号登录");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			if (ul == null) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "no-param", "参数有误!"));
				return resMap;
			}
			if (StringUtils.isEmpty(ul.getPwd()) || StringUtils.isEmpty(ul.getPhone())) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "no-param", "参数有误!"));
				return resMap;
			}
			ResultInfo ri = ValidateUtil.phone(ul.getPhone());
			if (ri != null) {
				resMap.put("result", ri);
				return resMap;
			}

			resMap = userService.updatePwdToLogin(ul);

		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
		}
		this.logAfter(logger);
		return resMap;
	}

	/**
	 * app用户注册
	 * @param ul UserLogin
	 * @param request request
	 * @return Map<String, Object>
	 */
	@RequestMapping(value = "register", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> register(UserLogin ul, HttpServletRequest request) {
		this.logBefore(logger, "用户注册");
		Map<String, Object> resMap = new HashMap<String, Object>();
		String type = request.getParameter("type");
		String channel = request.getParameter("channel");
		try {
			ResultInfo ri = ValidateUtil.phone(ul.getPhone());
			if (ri != null) {
				resMap.put("result", ri);
				return resMap;
			}
			ri = ValidateUtil.smsCode(ul.getSmsCode());
			if (ri != null) {
				resMap.put("result", ri);
				return resMap;
			}
			if (StringUtils.isEmpty(ul.getPwd())) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "error", "缺少参数！"));
				return resMap;
			}
			if (StringUtils.isEmpty(type) || StringUtils.isEmpty(channel)) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "error", "参数有误！"));
				return resMap;
			}
			/*if (StringUtils.isEmpty(ul.getParentName())) {
                ul.setParentName("system");
			}*/
			resMap = userService.saveUser(ul, type, channel);
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
		}
		this.logAfter(logger);
		return resMap;
	}

	/**
	 * 忘记密码
	 * @param ul UserLogin
	 * @param type type
	 * @param request request
	 * @return Map<String, Object>
	 */
	@RequestMapping(value = "updatePwd", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> updatePwd(UserLogin ul, String type, HttpServletRequest request) {
		this.logBefore(logger, "忘记密码");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			ResultInfo ri = ValidateUtil.phone(ul.getPhone());
			if (ri != null) {
				resMap.put("result", ri);
				return resMap;
			}
			ri = ValidateUtil.smsCode(ul.getSmsCode());
			if (ri != null) {
				resMap.put("result", ri);
				return resMap;
			}
			if (StringUtils.isEmpty(type)) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "error", "参数有误！"));
				return resMap;
			}
			resMap = userService.updateUserPwd(ul, type);

		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
		}
		this.logAfter(logger);
		return resMap;
	}
	
	/**
	 * app微信用户和系统账号绑定登录
	 * @param openid openid
	 * @param ul ul
	 * @param request request
	 * @param response response
	 * @return Map<String, Object>
	 */
	@RequestMapping(value = "loginBinding", method = {RequestMethod.POST, RequestMethod.GET})
	@ResponseBody
	public Map<String, Object> loginBinding(String openid, UserLogin ul, HttpServletRequest request, HttpServletResponse response) {
		this.logBefore(logger, "微信用户和系统账号绑定---开始");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			this.logInfo(logger, "参数openid=" + openid);
			if (StringUtils.isEmpty(openid)) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "no-param", "参数openid不能为空"));
				resMap.put("code", "no-param");
				return resMap;
			}
			if (ul == null) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "no-param", "参数账号信息有误!"));
				resMap.put("code", "no-param");
				return resMap;
			}
			if (StringUtils.isEmpty(ul.getPwd()) || StringUtils.isEmpty(ul.getPhone())) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "no-param", "参数账号信息有误!"));
				resMap.put("code", "no-param");
				return resMap;
			}
			this.logInfo(logger, "phone=" + ul.getPhone());
			ResultInfo ri = ValidateUtil.phone(ul.getPhone());
			if (ri != null) {
				resMap.put("result", ri);
				resMap.put("code", ri.getCode());
				return resMap;
			}
			
			resMap = userService.updatePwdToWeChatLogin("0", ul, openid);

		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
			resMap.put("code", "expection");
		}
		this.logAfter(logger);
		return resMap;
	}
	/**
	 * app微信用户登录
	 * @param openid openid
	 * @param request request
	 * @param response response
	 * @return Map<String, Object>
	 */
	@RequestMapping(value = "wechatlogin", method = {RequestMethod.POST, RequestMethod.GET})
	@ResponseBody
	public Map<String, Object> wechatlogin(String openid, HttpServletRequest request, HttpServletResponse response) {
		this.logBefore(logger, "微信用户登录---开始");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			this.logInfo(logger, "参数openid=" + openid);
			if (StringUtils.isEmpty(openid)) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "no-param", "参数openid不能为空"));
				resMap.put("code", "no-param");
				return resMap;
			}
			resMap = userService.weChatLogin("0", openid);

		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
			resMap.put("code", "expection");
		}
		this.logAfter(logger);
		return resMap;
	}
	
	/**
	 * app用户注册(微信)
	 * @param openid openid
	 * @param ul ul
	 * @param request request
	 * @return Map<String, Object>
	 */
	@RequestMapping(value = "registerForWeChat", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> registerForWeChat(String openid, UserLogin ul, HttpServletRequest request) {
		this.logBefore(logger, "用户注册(微信)");
		Map<String, Object> resMap = new HashMap<String, Object>();
		String type = request.getParameter("type");
		String channel = request.getParameter("channel");
		try {
			if (StringUtils.isEmpty(openid)) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "no-param", "参数openid不能为空"));
				resMap.put("code", "no-param");
				return resMap;
			}
			ResultInfo ri = ValidateUtil.phone(ul.getPhone());
			if (ri != null) {
				resMap.put("result", ri);
				resMap.put("code", ri.getCode());
				return resMap;
			}
			ri = ValidateUtil.smsCode(ul.getSmsCode());
			if (ri != null) {
				resMap.put("result", ri);
				resMap.put("code", ri.getCode());
				return resMap;
			}
			if (StringUtils.isEmpty(ul.getPwd())) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "error", "缺少参数！"));
				resMap.put("code", "error");
				return resMap;
			}
			if (StringUtils.isEmpty(type) || StringUtils.isEmpty(channel)) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "error", "参数有误！"));
				resMap.put("code", "error");
				return resMap;
			}
			resMap = userService.saveUserFromWeChat("0", ul, type, openid,channel);
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
			resMap.put("code", "expection");
		}
		this.logAfter(logger);
		return resMap;
	}
}
